package com.teziyou.commodity.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mytravel.commodityBridge.service.bean.AdvicedScenicspotCategory;
import com.teziyou.commodity.enumtype.CategoryEnum;
import com.teziyou.commodity.logic.CategoryLogic;
import com.teziyou.commodity.logic.ShelfLogic;
import com.teziyou.commodity.logic.TicketLogic;
import com.teziyou.commodity.service.DictService;
import com.teziyou.commodity.service.SceneService;
import com.teziyou.commodity.util.Maps;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/sceneType")
@Controller
public class SceneTypeController extends BaseController {
	
	@Autowired
	private TicketLogic ticketLogic;
	@Autowired
	private ShelfLogic shelfLogic;
	@Autowired
	private DictService dictService;
	@Autowired
	private CategoryLogic categoryLogic;
	@Autowired
	private SceneService sceneService;
	
	@RequestMapping("/index")
	public String index(Map map) {
		map.put("trees", JSON.toJSONString(categoryLogic.getCategoryTree(CategoryEnum.SCENETHEME.getCode())));
		
		Map<String,String> condition = new HashMap<String,String>();
		condition.put("isOK", "1");
		map.put("treesTarget", JSON.toJSONString(this.getTreeTarget(sceneService.findScenicspots(condition))));
		return "scenetype/index";
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public String save(HttpServletRequest req) throws JSONException {

		String data = req.getParameter("data");
		List<String>  list = JSON.parseArray(data, String.class);
		List<AdvicedScenicspotCategory> listSend = new ArrayList<AdvicedScenicspotCategory>();
		
		AdvicedScenicspotCategory adviceSpot = null; 
		for (String item: list){
			adviceSpot = new AdvicedScenicspotCategory();
			adviceSpot.setDictionaryId(Integer.parseInt(item.trim()));
			adviceSpot.setIsok(1);
			listSend.add(adviceSpot);
		}	
		boolean operJudge =  sceneService.addScenicspots(listSend);

		if (!operJudge) {
			return success("添加成功！！");
		}else {
			return fail("添加失败！！");
		}
		
	}
	
	@ResponseBody
	@RequestMapping("/del")
	public String delete(HttpServletRequest req) {
		String data = req.getParameter("data");
		List<String>  list = JSON.parseArray(data, String.class);
		List<Integer> listSend = new ArrayList<Integer>();	
		Map<String,String> condition = new HashMap<String,String>();
		condition.put("isOK", "1");
		String resultString = sceneService.findScenicspots(condition);
		JSONArray jsonArray = JSONObject.parseArray(resultString);
		
		for (String item: list){
			for(int i = 0; i < jsonArray.size(); i ++){
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				Integer dictionaryId = (Integer) jsonObject.get("dictionary_id");
				if (dictionaryId != null && dictionaryId.toString().equals(item.trim())) {
					listSend.add(jsonObject.getInteger("advicedspot_category_id"));
				}
			}
		}
		boolean operJudge = sceneService.deleteScenicspotsById(listSend);

		if (!operJudge) {
			return success("删除成功！！");
		}else {
			return fail("删除失败！！");
		}	
	}
	
	public List getTreeTarget(String resultString) {
		JSONArray jsonArray = JSONObject.parseArray(resultString);
		List treeList = new ArrayList();
		if(jsonArray!=null) {
			for(int i = 0; i < jsonArray.size(); i ++){
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				Integer dictionaryId = (Integer) jsonObject.get("dictionary_id");
				String name =  (String) jsonObject.get("Name");
				
				treeList.add(Maps.newHashMap("id", dictionaryId, "pId", dictionaryId, "name", name, "open", true));

			}
		}
		return treeList;
	
	}
}
